package com.xiaoxiang.game.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxiang.common.result.HttpResult;
import com.xiaoxiang.game.entity.Game;
import com.xiaoxiang.game.service.GameService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Game)表控制层
 *
 * @author haha
 * @since 2022-03-25 18:41:54
 */
@RestController
public class GameController {
    /**
     * 服务对象
     */
    @Resource
    private GameService gameService;


    /**
     * 获取所有热点游戏集合
     *
     */
    @GetMapping("hot/list")
    public HttpResult getHotGameList(@RequestParam(value = "size", defaultValue = "5")
                                                 Integer size) {
        return HttpResult.success(this.gameService.getHotList(size));
    }

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param game 查询实体
     * @return 所有数据
     */
    @GetMapping("list")
    public HttpResult selectAll(Page<Game> page, Game game) {
        return HttpResult.success(this.gameService.page(page, new QueryWrapper<>(game)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping
    public HttpResult selectOne(@RequestParam Long id) {
        return HttpResult.success(this.gameService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param game 实体对象
     * @return 新增结果
     */
    @PostMapping
    public HttpResult insert(@RequestBody Game game) {
        return HttpResult.success(this.gameService.save(game));
    }

    /**
     * 修改数据
     *
     * @param game 实体对象
     * @return 修改结果
     */
    @PutMapping
    public HttpResult update(@RequestBody Game game) {
        return HttpResult.success(this.gameService.updateById(game));
    }

    /**
     * 删除数据
     *
     * @return 删除结果
     */
    @DeleteMapping
    public HttpResult delete(@RequestParam("id") Long id) {
        return HttpResult.success(this.gameService.removeById(id));
    }
}
